
# coding:utf-8

import flux

class ViewGroup(object):

    def SetHUD(self, value):
        for k,v in vars(self).items():
            if isinstance(v, flux.View):
                v.SetHUD(value)
            elif isinstance(v, ViewGroup):
                v.SetHUD(value)

    def AddToScreen(self, scr, layer=0):
        for k,v in vars(self).items():
            _layer = layer
            if 'layer_offset' in dir(v):
                _layer += v.layer_offset
            if isinstance(v, flux.View):
                scr.AddView(v, _layer)
            elif isinstance(v, ViewGroup):
                v.AddToScreen(scr, _layer)

    def SetVisible(self, value):
        for k,v in vars(self).items():
            if isinstance(v, flux.View):
                v.SetVisible(value)
            elif isinstance(v, ViewGroup):
                v.SetVisible(value)

    def GetVisible(self):
        _ = True
        for k,v in vars(self).items():
            if isinstance(v, flux.View):
                _ = _ and v.GetVisible()
            elif isinstance(v, ViewGroup):
                _ = _ and v.GetVisible()
        return _

    def SetLayer(self, layer):
        for k,v in vars(self).items():
            if isinstance(v, flux.View):
                v.SetLayer(layer)

    def __getitem__(self, index):
        return vars(self)[index]

    def __setitem__(self, index, value):
        vars(self)[index] = value

    def __iter__(self):
        for k,v in vars(self).items():
            if isinstance(v, flux.View):
                yield k, v
            elif isinstance(v, ViewGroup):
                yield k, v


def singleton(cls):
  instances = {}
  def get_instance():
    if cls not in instances:
      instances[cls] = cls()
    return instances[cls]
  return get_instance
  